/**
 * Created by liujie on 2017/11/12.
 */
var express = require('express');
var request = require('request');
var Mock = require('mockjs');
var url = require('url');
var qstr = require("querystring");
var fs = require("fs");
var pro = require('../../property/cuspro');
var auth = require('../../auth/auth');
var util = require('../../util/commonutil');
var router = express.Router();
var resultJSON = require('../common/resultJSON');

router.get('/',function (req, res, next) {
    res.render("report/jingqingreportManage");
});

router.post('/queryDuanDianReport',function (req,res,next) {
    var jsonResult = {
        total:0,
        rows:[],
        isSuccess:false,
        message:""
    };
    var sid=auth.getSessionID(req, res, next);
    var qs=req.body;
    if(!qs.waringstate){
        qs.waringstate = 0;
    }
    qs.sid = sid;
    if(qs.isSearch){
        request({
            url:pro.devServeraddr + "heb/tabWaringInfo/list",
            method: "POST",
            qs:qs,
        },function (error, response, body) {
            if(!error && response.statusCode == 200){
                try{
                    var result = JSON.parse(body);

                    if(result.success == true){
                        jsonResult.total = result.total;
                        jsonResult.rows = result.rows;
                        jsonResult.isSuccess = true;
                        jsonResult.carTotal = result.carTotal
                    }else{
                        jsonResult.isSuccess = false;
                        jsonResult.message = result.msg ? result.msg : "断电报警查询数据异常，请稍后重试！";
                    }
                }catch (e){
                    jsonResult.isSuccess = false;
                    jsonResult.message = "断电报警查询数据异常，请稍后重试！";
                    console.log(e.message)
                }
            }else{
                jsonResult.isSuccess = false;
                jsonResult.message = "断电报警查询数据异常，请稍后重试！";
            }
            res.json(jsonResult);
        });
    }else{
        var tableResult = {
            total:0,
            rows:[],
            isSuccess:true,//必须是boolean类型
            message:""
        };
        res.json(tableResult);
    }


});

/**
 * 查询报警信息
 */
router.post('/listByCarWaring',function (req,res,next) {
    request({
        url:pro.devServeraddr + "heb/realGpsInfo/listByCarWaring",
        method: "POST"
    },function (error, response, body) {
        console.log("报警信息--"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);
            var jsonResult = {
                total:0,
                rows:[],
                isSuccess:false,
                message:""
            };
            if(result.success == true){
                jsonResult.total = result.rows.length;
                jsonResult.rows = result.rows;
                jsonResult.isSuccess = true;
            }else{
                jsonResult.isSuccess = false;
                jsonResult.message = result.msg ? result.msg : "离线表格查询数据异常，请稍后重试！";
            }
        }else{
            jsonResult.isSuccess = false;
            jsonResult.message = "离线表格查询数据异常，请稍后重试！";
        }
        res.json(jsonResult);
    });

    // res.json(treeNodes)
});
/**
 * 处理报警信息
 */
router.post('/updateWarningType',function (req,res,next) {
    var id = req.param("id");
    var type =1;
    request({
        url:pro.devServeraddr + "heb/tabWaringInfo/updateType",
        method: "POST",
        qs:{
            "id":id,
            "type":type
        }
    },function (error, response, body) {
        console.log("处理报警信息--"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);
            resultJSON.data = result
        }else{
            resultJSON.isSuccess = false;
            resultJSON.message = "离线表格查询数据异常，请稍后重试！";
        }
        res.json(resultJSON);
    });

    // res.json(treeNodes)
});
module.exports =router;